package com.free.semantic.core.common.serializeimpl.baseimpl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.free.semantic.common.RelationItem;
import com.free.semantic.core.RelationFunction;
import com.free.semantic.core.common.serializeimpl.BaseSerializeImpl;
import com.free.semantic.core.script.impl.ScriptEngineImpl;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class cURLRelationImpl implements BaseSerializeImpl {

    private List<String> commandLine;

    static {
        ScriptEngineImpl.registerScriptSolver("cURL",
                (conceptInstanceManage, strings) -> {
                    String relation = strings.get(0);
                    RelationItem relationItem = RelationItem.parseFromStr(relation);
                    conceptInstanceManage.registerFunction(relationItem.getSourceObjectList(),
                            relationItem.getDestObject(),
                            new HttpRequestImpl(strings));
                });
    }

    @Override
    public JSONObject getParameter() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("commandLine", commandLine);
        return jsonObject;
    }

    @Override
    public RelationFunction fromParam(JSONObject parameter) {
        JSONArray commandLine1 = parameter.getJSONArray("commandLine");
        List<String> commandLine = new ArrayList<>();
        commandLine1.forEach(s -> {
            commandLine.add(s.toString());
        });
        return new HttpRequestImpl(commandLine);
    }
}
